{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e0467fd0-c5a4-4b8a-b364-70de8541ff53",
   "metadata": {},
   "source": [
    "### Try this notebook in Google Colab, Binder or SageMaker!\n",
    "\n",
    "[![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/InsightSoftwareConsortium/itkwidgets/blob/main/examples/integrations/itk/DICOM.ipynb)\n",
    "[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/InsightSoftwareConsortium/itkwidgets/HEAD?labpath=examples%2Fintegrations%2Fitk%2FDICOM.ipynb)\n",
    "[![Open In SageMaker Studio Lab](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github.com/InsightSoftwareConsortium/itkwidgets/blob/main/examples/integrations/itk/DICOM.ipynb)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "59ad4f8f-8342-436c-95f6-d7a5ba35c964",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Install dependencies for this example\n",
    "import sys\n",
    "\n",
    "!{sys.executable} -m pip install -q --upgrade \"itk-io>=5.3.0\" tqdm pooch \"itkwidgets[all]>=1.0a23\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "b99a02af-1267-4212-8bad-69398037c8ec",
   "metadata": {},
   "outputs": [],
   "source": [
    "from itkwidgets import view\n",
    "\n",
    "import pooch\n",
    "import itk\n",
    "from pathlib import Path"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d140f3f6-c670-4c59-90e0-2d11a0cfe676",
   "metadata": {},
   "outputs": [],
   "source": [
    "dicom_files = pooch.retrieve('https://data.kitware.com/api/v1/item/6347159711dab81428208e24/download',\n",
    "                             known_hash='7ae09068c9b807b2276feb3427cebf82865ae39efc954f0222a4eec1acd6554d',\n",
    "                             processor=pooch.Unzip(),\n",
    "                             progressbar=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "17a6a0fd-0771-455e-8f50-67d54f44642d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ITK will sort the DICOM series into a volume properly using spatial metadata\n",
    "# Pass the directory containing the DICOM series to itk.imread\n",
    "image = itk.imread(Path(dicom_files[0]).parent)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "37e419b9-4728-48d9-bd9c-f282f6b57a5e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "                <img id=\"screenshot_imjoy_window_ff9ae1c8-bc71-4344-b184-64021e859ea7\" src=>\n",
       "                <script type=\"text/javascript\">\n",
       "                    var image = document.getElementById(\"screenshot_imjoy_window_ff9ae1c8-bc71-4344-b184-64021e859ea7\");\n",
       "                    image.src = \"\";\n",
       "                    var viewer = document.getElementById(\"imjoy_window_ff9ae1c8-bc71-4344-b184-64021e859ea7\");\n",
       "                    // Hide the static image if the Viewer is visible\n",
       "                    image.style.display = viewer ? \"none\" : \"block\";\n",
       "                </script>\n",
       "            "
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/javascript": [
       "window.connectPlugin && window.connectPlugin(\"d2513260-ac0a-4695-bf33-ddb78f1a3e91\")"
      ],
      "text/plain": [
       "<IPython.core.display.Javascript object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div id=\"eb9a9d50-fddf-4dd2-b985-558e45daac58\"></div>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "<itkwidgets.viewer.Viewer at 0x7f213851bd90>"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "view(image)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "feed805d-538d-4c47-89c6-8a3c74cfe2b3",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
